package com.ton.utils.quartz;

import com.hai.admin.table.vo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;

/**
 * 工作阻塞队列添加 任务的工具类
 */
@Component
@Slf4j
public class QueueUtils {

    @Resource
    BlockingQueue<FutureTask<Object>> blockingQueue;

    // 提交任务到队列，并返回 FutureTask 对象
    public <T> FutureTask<T> submit(Callable<T> task) {
        FutureTask<T> futureTask = new FutureTask<>(task);
        try {
            blockingQueue.put((FutureTask<Object>) futureTask);
        } catch (InterruptedException e) {
            log.error("任务提交到队列失败: {}", e.getMessage());
            Thread.currentThread().interrupt();
        }
        return futureTask;
    }

}
